<?php
class cls_data_user extends cls_data_base
{
	var $table_name = '`administor`';
	
	function check_login($user_name, $password)
	{
		$administor_info = $this->get_user_info($user_name);
		if (empty($administor_info)) {
			return return_info(false, '用户名不存在');
		}
		
		if ($administor_info['password'] == md5($password)) {
			return return_info(true, $administor_info);
		} else {
			return return_info(false, '密码错误');
		}
	}
	
	
	function get_user_list()
	{
		$sql = "select a.*, group_concat(p.permission_name) permission
				from {$this->table_name} a 
					left join allocation_permission ap on a.administor_id = ap.administor_id and ap.is_valid = 1
					left join permission p on ap.permission_id = p.permission_id
				group by a.user_name
				order by administor_id
			";
		$user_list = $this->db->getAll($sql);
		return $user_list;
	}
	
	function get_user_info($user_name)
	{
		$user_name = mysql_real_escape_string($user_name);
		$sql = "select a.*, group_concat(p.permission_name) permission
				from {$this->table_name} a 
					left join allocation_permission ap on a.administor_id = ap.administor_id and ap.is_valid = 1
					left join permission p on ap.permission_id = p.permission_id
				where a.user_name = '{$user_name}' 
				group by a.user_name
			";
		$user_info = $this->db->getRow($sql);
		return $user_info;
	}
	
	function update($user_name, $value)
	{
		$user_name = mysql_real_escape_string($user_name);
		foreach ($value as $k=>$v) {
			$value[$k] = mysql_real_escape_string($v);
		}
		$this->db->update($this->table_name, $value, "user_name = '{$user_name}'", 1);
		return return_info();
	}
	
	function create($value)
	{
		foreach ($value as $k=>$v) {
			$value[$k] = mysql_real_escape_string($v);
		}
		$value['register_time'] = get_now();
		$this->db->insert($this->table_name, $value);
		return return_info();
	}
	
	function update_lastlogin($user_name)
	{
		$user_name = mysql_real_escape_string($user_name);
		$sql = "update administor set last_login_time=now() where binary user_name='{$user_name}'";
		$this->db->query($sql);
	}
}